package cn.com.git.admin.api;

import cn.com.git.admin.common.UpmsConstant;
import cn.com.git.admin.service.UpmsService;
import cn.com.git.admin.service.dto.AuthenticationDTO;
import cn.com.git.admin.service.dto.AuthenticationTokenDTO;
import cn.com.git.common.base.BaseApi;
import cn.com.git.common.model.ResultMsg;
import cn.com.git.common.util.object.JSONUtils;
import cn.com.git.common.util.object.MapperFacadeUtils;
import cn.com.git.common.util.security.JwtUtils;
import com.google.common.collect.Maps;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotNull;

/**
 * 统一鉴权管理
 * Created by shuzheng on 2016/12/10.
 */
@Api(value = "鉴权管理", description = "鉴权管理")
@RestController
@RequestMapping("/upms")
public class UpmsApi extends BaseApi {

    @Value("${jwt.token.key}")
    private String jwtKey;

    @Autowired
    private UpmsService upmsService;


    @ApiOperation(value = "登录-用户登录-密码登录", httpMethod = "POST", notes = "返回用户token")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "account", value = "账号", required = true, example = "18396124752", paramType = "form", dataType = "string"),
            @ApiImplicitParam(name = "password", value = "密码", required = true, example = "856321", paramType = "form", dataType = "string")
    })
    @PostMapping(value = "/sso/login/password",consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResultMsg<AuthenticationDTO> loginByPassword(@NotNull(message = "账号不能为空") String account,
                                                             @NotNull(message = "密码不能为空") String password) {
        ResultMsg<AuthenticationDTO> resultMsg = upmsService.loginByPassword(account,password);
        if(resultMsg.isSuccess()){
            //token 组装
            AuthenticationDTO authenticationDTO = resultMsg.getData();
            AuthenticationTokenDTO authenticationTokenDTO = new AuthenticationTokenDTO();
            authenticationTokenDTO.setSysUser(MapperFacadeUtils.map(authenticationDTO.getSysUser(),AuthenticationTokenDTO.SysUser.class));
            String token = createToken(authenticationTokenDTO);
            authenticationDTO.setToken(token);
            resultMsg.setData(authenticationDTO);
        }
        return resultMsg ;
    }

    private String createToken(AuthenticationTokenDTO authenticationDTO){
        return JwtUtils.createToken(UpmsConstant.UPMS_CLIENT_SESSION_TIMEOUT, Maps.newHashMap(),"upms",authenticationDTO.getSysUser().getAccount(),JSONUtils.toJson(authenticationDTO),jwtKey);
    }
}